package com.codi.bus.core.service.organ.impl;

import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.constant.RuleConstant;
import com.codi.bus.core.dao.LookupDao;
import com.codi.bus.core.dao.OrganBankDao;
import com.codi.bus.core.dao.OrganProfileDao;
import com.codi.bus.core.domain.Lookup;
import com.codi.bus.core.domain.OrganBank;
import com.codi.bus.core.domain.OrganProfile;
import com.codi.bus.core.service.organ.OrganBankService;
import com.codi.bus.exception.BaseException;
import com.codi.message.constant.ErrorConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author lvgo
 * @version 1.0
 * @Description: 机构用户银行卡
 * @date 2018/5/8 14:13
 */
@Service
public class OrganBankServiceImpl implements OrganBankService {

    @Resource
    private OrganBankDao organBankDao;

    @Resource
    private OrganProfileDao organProfileDao;

    @Resource
    private LookupDao lookupDao;

    @Override
    public OrganBank selectByTradeAccount(String tradeAccount) {
        List<OrganBank> organBankList = organBankDao.selectByTradeAccount(tradeAccount);
        if (!organBankList.isEmpty()) {
            return organBankList.get(0);
        }
        return null;
    }

    @Override
    public List<OrganBank> getAll(String profileName, String state, Integer pageIndex, Integer pageSize) {
        return organBankDao.getAll(profileName, state, pageIndex, pageSize);
    }

    /**
     * 根据企业id获取银行卡信息
     *
     * @param profileId
     * @return
     */
    @Override
    public List<OrganBank> getListByProfileId(String profileId) {
        return organBankDao.selectUsableByProfileId(Long.valueOf(profileId));
    }
    /**
     * 根据企业id获取全部银行卡信息
     *
     * @param profileId
     * @return
     */
    @Override
    public List<OrganBank> selectAllListByProfileId(String profileId) {
        return organBankDao.selectAllListByProfileId(Long.valueOf(profileId));
    }


    /**
     * 添加 修改 银行卡
     *
     * @param profileId   机构id
     * @param bankAccount 银行账户
     * @param bankName    银行名称
     * @param bankNo      银行账号
     * @param branchName  支行名称
     */
    @Override
    public OrganBank addOrganBank(String organBankId, String userId, String profileId, String tradePassword, String bankAccount, String bankName, String bankNo, String branchName, String bankCode, String branchBankCode) {
        OrganProfile organProfile = organProfileDao.selectByPrimaryKey(Long.valueOf(profileId));
        if (organProfile == null) {
            throw new BaseException(ErrorConstant.ORGAN_PROFILE_NULL);
        }
        if (!StringUtils.equals(organProfile.getUserId().toString(), userId)) {
            throw new BaseException(ErrorConstant.USER_MATCHING_ORGAN);
        }
        // 获取恒生银行编号
        String bankHSCode = "";
        bankCode = bankCode.toUpperCase();
        List<Lookup> lookups = lookupDao.getLookupByType(RuleConstant.RULE_LOOKUP_TYPE_HSBANK);
        for (Lookup lookup : lookups) {
            if (lookup.getComponentValue().equals(bankCode)) {
                bankHSCode = lookup.getRuleValue();
                break;
            }
        }

        // 是否是修改记录
        boolean flag = false;
        OrganBank organbank = null;
        if (StringUtils.isNotBlank(organBankId)) {
            organbank = organBankDao.selectByPrimaryKey(Long.valueOf(organBankId));
        }
        // 新增银行卡
        if (organbank == null) {
            organbank = new OrganBank();
            organbank.setCreateTime(new Date());
            organbank.setCreateUserId(Integer.valueOf(userId));
            // 修改银行卡
        } else {
            flag = true;
            organbank.setLastupdateTime(new Date());
            organbank.setLastupdateUserId(Integer.valueOf(userId));
        }

        // 对银行卡限制进行校验  修改银行卡过滤掉自身条件
        /*
            1. 支持所有银行类型卡绑定
            2. 一个银行下只能绑定一张银行卡
         */

        List<OrganBank> organBanks = organBankDao.selectAllListByProfileId(Long.valueOf(profileId));
        for (OrganBank organBank : organBanks) {
            // 修改银行卡过滤
            if (flag && organBank.getUbBankCode().equals(bankCode) && !organBank.getOrganBankId().toString().equals(organBankId)) {
                throw new BaseException(ErrorConstant.ERROR_CHANNEL_SAME_BANK_MAX_ONE);
            }
            // 新增银行卡过滤
            if (!flag && organBank.getUbBankCode().equals(bankCode)) {
                throw new BaseException(ErrorConstant.ERROR_CHANNEL_SAME_BANK_MAX_ONE);
            }
        }
        organbank.setUbBranchbank(branchBankCode);
        organbank.setUserId(Long.valueOf(userId));
        organbank.setProfileId(Long.valueOf(profileId));
        organbank.setTradePassword(tradePassword);
        organbank.setUbBankName(bankName);
        organbank.setUbOrganBankNo(bankNo);
        organbank.setUbBankNo(bankHSCode);
        organbank.setUbBankCode(bankCode);
        organbank.setUbBankBranchName(branchName);
        // 银行卡所有者
        organbank.setUbOrganBankOwner(bankAccount);
        // 资金方式
        organbank.setUbCapitalMode(GlobalConstant.CAPTIAL_MODE_K);
        // 审核状态 ：0 未审核 1 审核通过
        organbank.setAuditStatus(0);


        if (flag) {
            organBankDao.updateByPrimaryKey(organbank);
        } else {
            organBankDao.insert(organbank);
        }
        return organbank;
    }

    /**
     * 通过id查询银行卡信息
     *
     * @param aLong
     * @return
     */
    @Override
    public OrganBank getById(Long aLong) {
        return organBankDao.selectByPrimaryKey(aLong);
    }

    /**
     * 更新银行卡信息
     *
     * @param organBank
     */
    @Override
    public void updateOrganBank(OrganBank organBank) {
        organBankDao.updateByPrimaryKey(organBank);
    }
}
